let sort = (items, value, type = 'sort') => { // item: 数据数组, value: 根据值排序, type: 正排或倒排
  if (type === 'sort') {
    items.sort((a, b) => {
      if (a[value] > b[value]) {
        return -1
      }
      if (a[value] < b[value]) {
        return 1
      }
    })
    return items
  } else {
    items.sort((a, b) => {
      if (a[value] < b[value]) {
        return -1
      }
      if (a[value] > b[value]) {
        return 1
      }
    })
    return items
  }
}

let sortByObj = (items, value, type = 'sort') => {
  let val = []
  let obj = {

  }
  Object.keys(items).forEach((item, idx) => {
    val.push(items[item])
    val[idx].key = item
  })
  val = sort(val, value, type)
  val.forEach((item, idx) => {
    obj[item.key] = item
  })
  return obj
}

/**
 * obj 用来排序的key值
 * 例: 
 * {
 *  mouth: 0,
 *  year: 0,
 *  day: 0
 * }
 * 
 * key 以该值作为排序的key
 */

let sortControl = (obj, key) => {
  if (obj[key] === 0) {
    Object.keys(obj).forEach((val) => {
      obj[val] = 0
    })
    obj[key]++
    return obj
  }
  obj[key] === 2 ? obj[key] -- : obj[key]++
  return obj
}

export { sort, sortControl, sortByObj }